package droideep.library.widget.menu;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import droideep.library.utils.MetricsConverter;
import droideep.library.widget.FloatingActionButton;

/**
 * @author goodev
 * @version 1.0
 * @date 15/5/24
 */
public class FloatingArcLayout extends ViewGroup {

    private static final String TAG = FloatingArcLayout.class.getSimpleName();
    private final float MINI_RADIUS = MetricsConverter.dpToPx(getContext(), 100f);
    private double mFromDegree = 180.0;
    private double mToDegree = 270.0;
    private float mChildSize = MetricsConverter.dpToPx(getContext(), 40.0f);
    private float mChildPadding = MetricsConverter.dpToPx(getContext(), 10.0f);
    private float mRadius;

    public FloatingArcLayout(Context context) {
        super(context);
    }

    public FloatingArcLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatingArcLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FloatingArcLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        // 计算圆的半径
        final float radius = mRadius = computeRadius(Math.abs(mToDegree - mFromDegree), getChildCount(), mChildSize, mChildPadding, MINI_RADIUS);
        final int size = (int) (radius * 2 + mChildSize);

        setMeasuredDimension(size, size);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (!changed) return;

        final int _CenterX = getWidth() / 2;
        final int _CenterY = getHeight() / 2;
        final int _ChildCount = getChildCount();
        final float _PerDegree = (float) ((mToDegree - mFromDegree) / (_ChildCount - 1));

        float _Degrees = (float) mFromDegree;
        for (int i = 0; i < _ChildCount; i++) {
            Rect _Frame = computeChildFrame(_CenterX, _CenterY, mRadius, _Degrees, mChildSize);
            _Degrees += _PerDegree;
            getChildAt(i).layout(_Frame.left, _Frame.top, _Frame.right, _Frame.bottom);
        }
    }

    private static Rect computeChildFrame(int pCenterX, int pCenterY, float pRadius, float pDegrees, float pChildSize) {
        final double childCenterX = pCenterX + pRadius * Math.cos(Math.toRadians(pDegrees));
        final double childCenterY = pCenterY + pRadius * Math.sin(Math.toRadians(pDegrees));

        return new Rect((int) (childCenterX - pChildSize / 2), (int) (childCenterY - pChildSize / 2),
                (int) (childCenterX + pChildSize / 2), (int) (childCenterY + pChildSize / 2));

    }

    private static float computeRadius(double pArcDegree, int pChildCount, float pChildSize, float pChildPadding, float pMiniRadius) {
        if (pChildCount < 2)
            return pMiniRadius;
        double perDegree = pArcDegree / (pChildCount - 1);
        double perHalfDegree = Math.toRadians(perDegree / 2);
        float perSize = pChildSize + pChildPadding;

        final float radius = (float) ((perSize / 2) / (Math.sin(perHalfDegree)));
        return Math.max(radius, pMiniRadius);
    }


}
